package cn.com.inossem.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.adapter.BaGuaAdapter;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.fragment.BaGuaFragment;
import cn.com.inossem.model.BaGuaListInfoBean;
import cn.com.inossem.pulltorefresh.widget.XListView;
import cn.com.inossem.pulltorefresh.widget.XListView.IXListViewListener;
import cn.com.inossem.requestmanager.WooRequestFactory;

import com.foxykeep.datadroid.requestmanager.Request;

public class MyMomentsActivity extends RequestActivity implements IXListViewListener{
	public static BaGuaFragment mf = null;
	private XListView mMessagesList;
	private BaGuaAdapter adapter = null;
	private boolean isRefresh = false;
	int page = 1;
	boolean shuaxinflag = true;
	String key = "";
	TextView title;
	public boolean mHasLoadedOnce = false;
	
	private Handler handler = new Handler() {

		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			switch (msg.what) {
			case 2:
				int position = (Integer) msg.getData().get("position");
				Intent intent = new Intent(MyMomentsActivity.this, BaGuaDetailsActivity.class);
				intent.putExtra("position", position);
				startActivityForResult(intent, 0);
				break;
			default:
				break;
			}
		}

	};

	private Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			adapter.notifyDataSetChanged();
			mMessagesList.stopRefresh();
			mMessagesList.stopLoadMore();
			mMessagesList.setRefreshTime(getTime());
			return false;
		}
	});

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		super.initAllMembers(savedInstanceState);

		title = (TextView) this.findViewById(R.id.title);

		mMessagesList = (XListView) this.findViewById(R.id.inspectionList);
		mMessagesList.setPullRefreshEnable(true);
		mMessagesList.setPullLoadEnable(true);
		mMessagesList.setXListViewListener(this);
		mMessagesList.setRefreshTime(getTime());

			initView();
	}

	public void initView() {
		ImageView back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);
		
		getInfo();
	}

	public void getInfo() {
		adapter = null;
		BaGuaFragment.messageInfo = new ArrayList<BaGuaListInfoBean>();
		launchRequest(WooRequestFactory.getMyMomentslist(key, page + "", ""));
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	private String getTime() {
		return new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(new Date());
	}

	@Override
	public int getContentViewId() {
		return R.layout.mymoments_layout;
	}

	// 处理返回值
	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		super.onRequestSucess(request, bundle);

		if (bundle.containsKey(WooRequestFactory.RESPONSE_BAGUALIST_DATA)) {
			List<BaGuaListInfoBean> retList = bundle
					.getParcelableArrayList(WooRequestFactory.RESPONSE_BAGUALIST_DATA);
			if (retList == null) {
				if (isRefresh) {
					mMessagesList.stopRefresh();
					mMessagesList.stopLoadMore();
					mMessagesList.setRefreshTime(getTime());
					Toast.makeText(this, "当前是最后一页！", Toast.LENGTH_LONG)
							.show();
					mMessagesList.setPullLoadEnable(false);
				}
				return;
			}
			if (retList.size() > 0) {
				// mInspections.addAll(retList);
				if (retList.size() < 10) {
					mMessagesList.setPullLoadEnable(false);
				}
				if (isRefresh) {
					mMessagesList.stopRefresh();
					mMessagesList.stopLoadMore();
					mMessagesList.setRefreshTime(getTime());
				}
				BaGuaFragment.messageInfo.addAll(retList);

				System.out.println("长度：" + BaGuaFragment.messageInfo.size());
				if (adapter == null) {
					adapter = new BaGuaAdapter(this, BaGuaFragment.messageInfo, handler, true);
					mMessagesList.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				} else {
					adapter.notifyDataSetChanged();
				}

			} else {
				mMessagesList.setPullLoadEnable(false);
				if (isRefresh) {
					mMessagesList.stopRefresh();
					mMessagesList.stopLoadMore();
					mMessagesList.setRefreshTime(getTime());
				}
				if (adapter != null) {
					adapter.notifyDataSetChanged();
				}
			}
		}

	}

	public void charu(String tabname, ContentValues contentValues,
			String whereClause) {
		DatabaseHelper dh = new DatabaseHelper(this);
		SQLiteDatabase db = dh.getWritableDatabase();

		db.update(tabname, contentValues, whereClause, null);

		db.close();
		dh.close();
	}

	@Override
	public void onRefresh() {

		isRefresh = true;
		mMessagesList.setPullLoadEnable(true);
		BaGuaFragment.messageInfo.clear();
		page = 1;
		launchRequest(WooRequestFactory.getMyMomentslist(key, page + "", ""));

	}

	@Override
	public void onLoadMore() {// 加载
		// if (Utils.isDenglu == 0) {
		// ((MainActivity) getActivity()).setPageView(3);
		// return;
		// }
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				isRefresh = true;
				page += 1;
				launchRequest(WooRequestFactory.getMyMomentslist(key, page + "", ""));
			}

		}, 2000);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.back:
				finish();
				break;
			default:
				break;
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			adapter.notifyDataSetChanged();
			break;
		case 2:
			mMessagesList.setPullLoadEnable(true);
			BaGuaFragment.messageInfo.clear();
			if (adapter != null) {
				adapter.notifyDataSetChanged();
			}
			page = 1;
			launchRequest(WooRequestFactory.getMyMomentslist(key, page + "", ""));
			break;
		case 3:
			key = intent.getStringExtra("key");
			title.setText(key);
			mMessagesList.setPullLoadEnable(true);
			BaGuaFragment.messageInfo.clear();
			if (adapter != null) {
				adapter.notifyDataSetChanged();
			}
			page = 1;
			launchRequest(WooRequestFactory.getMyMomentslist(key, page + "", ""));
			break;
		default:
			break;
		}
	}
}